package com.qf.efserver.discover.service.impl;

import com.qf.common.core.entity.Nutrition;
import com.qf.common.core.utils.TokenUtils;
import com.qf.common.minio.utils.MinioUtil;
import com.qf.common.redis.utils.RedisUtils;
import com.qf.efserver.discover.service.DiscoverService;
import com.qf.efserver.food.VO.RecommendFoodVO;
import com.qf.efserver.food.param.AddCustomizeNutritionParam;
import com.qf.efserver.food.param.UpdateCustomizeNutritionParam;
import com.qf.efserver.food.service.NutritionFoodService;
import com.qf.efserver.food.service.UserDefinedNutritionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * Author hehecheng
 * Date 2024/11/9
 * 祝我们都能遇见真诚的人
 * 见字如面
 */
@Service
public class DiscoverServiceImpl implements DiscoverService {

    @Resource
    private NutritionFoodService nutritionFoodService;

    @Resource
    private UserDefinedNutritionService userDefinedNutritionService;

    @Resource
    private MinioUtil minioUtil;

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;


    /**
     * 推荐食物
     *
     * @return
     */
    @Override
    public List<RecommendFoodVO> recommendFood() {
        return nutritionFoodService.recommendedFood();
    }

    /**
     * 最近推荐的食物
     *
     * @return
     */
    @Override
    public List<RecommendFoodVO> recentFood() {
        return nutritionFoodService.recentFood();
    }

    /**
     * 添加自定义的食物
     *
     * @param addCustomizeNutritionParam
     */
    @Override
    public void addCustomizeNutrition(AddCustomizeNutritionParam addCustomizeNutritionParam) {
        Integer uid = TokenUtils.getUidFromToken();
        Integer nutritionId = nutritionFoodService.addCustomNutrition(addCustomizeNutritionParam);
        userDefinedNutritionService.addUserDefinedNutrition(nutritionId, uid);
    }

    /**
     * 展示用户定制的食物
     *
     * @return
     */
    @Override
    public List<RecommendFoodVO> customizeNutritionList() {
        Integer uid = TokenUtils.getUidFromToken();
        String key = String.format("custom:food:%s", uid);
        List<RecommendFoodVO> customize = null;
        boolean b = RedisUtils.hasKey(key);
        if (b) {
            Map<Object, Object> map = RedisUtils.hashGetAll(key);
            Collection<Object> values = map.values();
            customize = values.stream().map(item -> {
                RecommendFoodVO recommendFoodVO = new RecommendFoodVO();
                Nutrition item1 = (Nutrition) item;
                BeanUtils.copyProperties(item1, recommendFoodVO);
                return recommendFoodVO;
            }).collect(Collectors.toList());
        } else {
            customize = nutritionFoodService.getUserCustomizeNutrition();
        }
        return customize;
    }

    /**
     * 用户修改自定义食物
     *
     * @param updateCustomizeNutritionParam
     */
    @Override
    public void updateCustomizeNutrition(UpdateCustomizeNutritionParam updateCustomizeNutritionParam) {
        nutritionFoodService.updateUserCustomizeNutrition(updateCustomizeNutritionParam);
    }

    /**
     * 删除用户自定义食物
     *
     * @param foodId
     */
    @Override
    public void delCustomizeNutrition(Integer foodId) {
        // 查询食物
        // Nutrition nutrition = nutritionFoodService.selectById(foodId);
//        String imgPath = nutrition.getImgPath();
//        if (!(imgPath.isEmpty() || imgPath.equals(" "))) {
//            String imgName = imgPath.substring(imgPath.lastIndexOf("/") + 1, imgPath.lastIndexOf("."));
//            threadPoolExecutor.execute(() -> {
//                minioUtil.deleteFile(imgName);
//            });
//        }
        nutritionFoodService.delUserCustomizeNutrition(foodId);
    }

    @Override
    public String upload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String newFileName = uuid + originalFilename.substring(originalFilename.lastIndexOf("."));
        String path = null;
        try {
            path = minioUtil.upload(file.getInputStream(), newFileName, file.getContentType());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return path;
    }
}
